
//备注类型1
function markType1(){
    var dom = $("#resolution-tmp-orderErp").html();
    p.layerOrderErp = new ComLayer({
        title:'同步ERP',
        dom:dom,
        okHandler:function(btnObj){
            if(p.skuID.checked().length<1){
                $.comMessage({
                    type:'prompt',
                    text:'请勾选sku!'
                })
                return;
            }
            var isTrue = $("#form-erp").valid();
            if(isTrue){
                reqMarkType('post','/api/erp/setErpOrderRemark',btnObj,getMarkPara1)
            }
        },
        callback:function(){
            renderSku();
            renderOption1();
            p.skuID = $("#skuID").comCheck();

            //$("[data-name='ProblemType']").comSelect({init:1});
            //$("[data-name='ComplaintChannel']").comSelect({init:true});
            $("[data-name='ControversyStage']").comSelect({init:true});
            //具体原因
            $("[data-name='reason']").comSelectFloor({
                data:filterReason(p.optionObj.data.ParentOrderProblem),
                multi:true,
            });


            $("#form-erp").validate({
                ignore: "",
                errorPlacement:function(error,element) {
                    element.closest(".sel-item").after(error);
                },
                rules: {
                    skuID:{required:true,},
                    reason:{required:true,},
                },
                messages: {
                    //skuID:{required:true,},
                    //reason:{required:true,}
                },
            });
        },
    })
}

//默认渲染数据
function renderOption1(){
    if(!p.optionObj.data){
        $.comMessage({
            type:'warning',
            text:'选项条件未获取！',
        });
        return;
    }
    var data = p.optionObj.data;
    var ComplaintChannel = "";
    var ProblemType = "";
    var ControversyStage= "";

    //投诉渠道
    $.each(data.ComplaintChannelList,function(index,item){
        ComplaintChannel+= '<li data-val="'+item.Value+'" class="sel-list">'+item.Text+'</li>';
    });
    $("[data-name='channelList']").html(ComplaintChannel);

    //原因类型
    $.each(data.ProblemTypeList,function(index,item){
        ProblemType+= '<li data-val="'+item.Value+'" class="sel-list">'+item.Text+'</li>';
    });
    $("[data-name='reasonTypeList']").html(ProblemType);

    //争议阶段
    $.each(data.OrderControversyStageList,function(index,item){
        ControversyStage+= '<li data-val="'+item.Value+'" class="sel-list">'+item.Text+'</li>';
    });
    $("[data-name='ControversyStageList']").html(ControversyStage);
}

//备注类型2
function markType2(){
    var dom = $("#resolution-tmp-orderErp2").html();
    p.layerOrderErp = new ComLayer({
        title:'退款备注',
        dom:dom,
        okHandler:function(btnObj){
            var isTrue = $("#form-erp2").valid();
            if(isTrue){
                reqMarkType('get','/api/getDispute/',btnObj)
            }
        },
        callback:function(){
            renderSku();
            $("[data-name='skuID']").comSelect({init:true});
            $("#form-erp2").validate({
                ignore: "",
                errorPlacement:function(error,element) {
                    element.closest(".sel-item").after(error);
                },
                rules: {
                    skuID:{required:true,},
                    refund:{required:true,},
                },
                messages: {},
            });
        },
    })
}



//备注类型3
function markType3(){
    var dom = $("#resolution-tmp-orderErp3").html();
    p.layerOrderErp = new ComLayer({
        title:'退货备注',
        dom:dom,
        okHandler:function(btnObj){
            var isTrue = $("#form-erp3").valid();
            if(isTrue){
                reqMarkType('get','/api/getDispute/',btnObj)
            }
        },
        callback:function(){
            //renderSku();
            //$("[data-name='skuID']").comSelect({init:true});
            $("#form-erp3").validate({
                rules: {
                    logistics:{required:true,},
                    orderNumber:{required:true,},
                },
                messages: {},
            });
        },
    })
}

//更改原因字段
function filterReason(reasonArr){
    var arr = [];
    $.each(reasonArr,function(index,item){
        var obj = {
            label:item.Name,
            value:item.Id,
            children:[]
        }
        if(item.ChildProblemData && item.ChildProblemData.length>=1){
            obj.children = filterReason(item.ChildProblemData)
        }
        arr.push(obj);
    });
    return arr;
}

//渲染sku
function renderSku(){
    if(!p.orderArr || p.orderArr.length<=0){
        $.comMessage({
            type:'warning',
            text:'未找到sku数据！',
        });
        return;
    }
    var skuStr = "";
    $.each(p.orderArr,function(index,item){
        //skuStr+= '<li data-val="'+item.SkuId+'" class="sel-list">'+item.SkuId+'</li>';
        skuStr+= '  <label style="margin-right: 10px;">' +
                    '    <i data-name="checkStyle" class="check-box"></i>' +
                    '    <span>'+item.SkuId+'</span>' +
                    '    <input type="checkbox" value="'+item.SkuId+'">' +
                    '</label>'
    });
    $("[data-name='orderSkuList']").html(skuStr);
}



//默认请求数据
function reqOption(){
    app.ajax({
        type:'get',
        url:app.api("/api/erp/getErpOrderBaseData"),
        success:function(res){
            if(res.code == 0){
                console.log(res)
                p.optionObj = res;
                $("#orderErp").prop('disabled',false)
            }
        }
    })
}

//获取列表参数
function getMarkPara1(){
    $.each($("#form-erp").serializeArray(),function(index,item){
        p.markData1[item.name] = item.value;
    });
    p.markData1.orderId = p.sendInfo.order_sn;
    p.markData1.skuID = p.skuID.checked();
    return p.markData1;
}

function reqMarkType(type,path,btnObj,getPara){
    app.ajax({
        type:type,
        url:app.api(path),
        data:getPara(),
        beforeSend:function(){
            btnObj.addClass('active-animate').prop('disabled',true)
        },
        success:function(res){
            if(res.code == 0){
                p.layerOrderErp.close();
            }
        },
        complete:function(){
            btnObj.removeClass('active-animate').prop('disabled',false)
        }
    })
}
